<template>
  <div class="lanjian-content">
    <div class='singleRow'>
      <a-form-model
        ref="ruleForm2"
        :rules="rules"
        class='formbox cusFormBox'
        :model="ruleForm"
        labelAlign="left"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-form-model-item
          label='审批状态'
          class='formtabMax'
        >
          <a-input
            v-model.trim="f_status"
            :maxLength='200'
            style="width: 50%"
            disabled="false"
          />
        </a-form-model-item>
        <a-divider />
        <a-form-model-item
          class='formtab upload'
        >
          <a-upload
            :multiple="false"
            :file-list="fileList_tb"
            :defaultFileList="ruleForm.fileList_tb"
            :customRequest="customUpload"
            :before-upload="beforeFileUpload"
            :showUploadList="true"
            :remove="handleRemove"
            maxCount="1"
            accept=".doc,.docx,.pdf,.xls,.xlsx,.jpg,.png,.jpeg,.txt,.et"
          ><span class="upload" style="color: #000000d9;font-size: 18px;font-weight: 700; margin-right: 10px"><span
            style="color: #ff5500;font-size: 18px;" >*</span>党内通报稿</span>
            <span><a-icon type="exclamation-circle" class="cio" /> <span class="tip">仅支持doc、docx、pdf、xls、xlsx、jpg、png、jpeg、txt、et文件格式、文件大小不超过200M。<br></span></span>
            <a-button class="download" @click.stop="downtmp(2)" type="default" title="下载模板">
              <span class='icon iconfont'>&#xe6c0;</span>
            </a-button>
            <a-divider type="vertical" />
            <a-button title="上传文件">
              <span class='icon iconfont'>&#xe677;</span>
            </a-button>
          </a-upload>
        </a-form-model-item>
        <a-form-model-item
          class='formtab upload'
        >
          <a-upload
            :multiple="false"
            :file-list="fileList_gk"
            :defaultFileList="ruleForm.fileList_gk"
            :customRequest="handleUploadGK"
            :before-upload="beforeFileUploadGK"
            :showUploadList="true"
            :remove="handleGKRemove"
            maxCount="1"
            accept=".doc,.docx,.pdf,.xls,.xlsx,.jpg,.png,.jpeg,.txt,.et"
          ><span style="color: #000000d9;font-size: 18px;font-weight: 700; margin-right: 10px"><span
            style="color: #ff5500;font-size: 18px;">*</span>社会公开报稿</span>
            <span><a-icon type="exclamation-circle" class="cio" /> <span class="tip">仅支持doc、docx、pdf、xls、xlsx、jpg、png、jpeg、txt、et文件格式、文件大小不超过200M。<br></span></span>
            <a-button class="download" @click.stop="downtmp(3)" type="default" title="下载模板">
              <span class='icon iconfont'>&#xe6c0;</span>
            </a-button>
            <a-divider type="vertical" />
            <a-button>
              <span class='icon iconfont' title="上传文件">&#xe677;</span>
            </a-button>
          </a-upload>
        </a-form-model-item>
        <a-form-model-item
          class='formtab upload'
        >
          <a-upload
            :multiple="false"
            :file-list="fileList_nogk"
            :defaultFileList="ruleForm.fileList_nogk"
            :customRequest="handleUploadNOGK"
            :before-upload="beforeFileUploadNOGK"
            :remove="handleNOGKRemove"
            maxCount="1"
            accept=".doc,.docx,.pdf,.xls,.xlsx,.jpg,.png,.jpeg,.txt,.et"
          ><span style="color: #000000d9;font-size: 18px;font-weight: 700; margin-right: 10px"><span
            style="color: #ff5500;font-size: 18px;">*</span>不宜向社会公开说明</span>
            <span><a-icon type="exclamation-circle" class="cio" /> <span class="tip">仅支持doc、docx、pdf、xls、xlsx、jpg、png、jpeg、txt、et文件格式、文件大小不超过200M。<br></span></span>
            <a-button class="download" @click.stop="downtmp(4)" type="default" title="下载模板">
              <span class='icon iconfont'>&#xe6c0;</span>
            </a-button>
            <a-divider type="vertical" />
            <a-button title="上传文件">
              <span class='icon iconfont'>&#xe677;</span>
            </a-button>
          </a-upload>
        </a-form-model-item>
        <a-divider />
        <a-form-model-item
          label='编辑人'
          class='formtab'
          prop='editor'
        >
          <a-input
            v-model.trim='ruleForm.editor'
            placeholder='请输入编辑人'
            :maxLength='50'
            style="width: 300px;"
          />
        </a-form-model-item>
        <div class="save">
          <a-button type="primary" @click="save" v-show="m_status!='2'&&m_status!='5'">保存</a-button>
        </div>
      </a-form-model>
    </div>

  </div>
</template>
<script>
import {
  getuploadFileInfo,
  addRectificationPlan,
  getRectificationPlanTotal,
  addRectificationZGGKTotal, getRectificationZGGKTotal, streamToBuffer, getJLID
} from '@/utils/JsonTools';
import UploadPlus from '@/components/UploadPlus/index.vue';
import {getAppLocalPath, getNowMilliSecond, getStatusType, getSystemDir} from "@/utils/common";
import {exportModule, getTemplete, mkdir} from "@/utils/fileUtils";
import fs from "fs";

export default {
  name: 'Total',
  components: { UploadPlus },
  data() {
    return {
      m_status: '',
      f_status: '',
      mulite: false,
      // 表单标题内容宽度设置
      labelCol: { span: 24 },
      wrapperCol: { span: 24 },
      ruleForm: { fileList_nogk: [], fileList_gk: [], fileList_tb: [] },
      // 表单规则
      rules: {
        fileList_tb: [{ required: true, message: '请上传文件', trigger: 'blur' }],
        fileList_gk: [{ required: true, message: '请上传文件', trigger: 'blur' }],
        fileList_nogk: [{ required: true, message: '请上传文件', trigger: 'blur' }],
        editor: [{ required: true, message: '请输入编辑人', trigger: 'blur' }],
      },
      fileList_gk: [],
      fileList_tb: [],
      fileList_nogk: []
    };
  },
  mounted(){
    this.getInfos()
    this.getstatus()
  },
  methods: {
    getStatusType,

    getNowMilliSecond,
    // 上传验证
    beforeFileUpload(file) {
      if (file.name.substring(0, file.name.indexOf('.')).length > 200) {
        this.$message.error('上传文件名字不得超过200字');
        return false;
      }
      let isLt2M = file.size / 1024 / 1024;
      if (isLt2M > 200) {
        this.$message.error('只允许上传200M以下的文件');
        return false;
      }
      let fileExt = file.name.slice(file.name.lastIndexOf('.') + 1);
      let fileTypeArr = ['doc', 'docx', 'pdf', 'xls', 'xlsx', 'jpg', 'png', 'jpeg', 'txt', 'et'];
      if (fileTypeArr.indexOf(fileExt) === -1) {
        this.$message.error('非法的文件类型');
        return false;
      }
      return true;
    },
    beforeFileUploadGK(file) {
      if (file.name.substring(0, file.name.indexOf('.')).length > 200) {
        this.$message.error('上传文件名字不得超过200字');
        return false;
      }
      let isLt2M = file.size / 1024 / 1024;
      if (isLt2M > 200) {
        this.$message.error('只允许上传200M以下的文件');
        return false;
      }
      let fileExt = file.name.slice(file.name.lastIndexOf('.') + 1);
      let fileTypeArr = ['doc', 'docx', 'pdf', 'xls', 'xlsx', 'jpg', 'png', 'jpeg', 'txt', 'et'];
      if (fileTypeArr.indexOf(fileExt) === -1) {
        this.$message.error('非法的文件类型');
        return false;
      }
      return true;
    },
    beforeFileUploadNOGK(file) {
      if (file.name.substring(0, file.name.indexOf('.')).length > 200) {
        this.$message.error('上传文件名字不得超过200字');
        return false;
      }
      let isLt2M = file.size / 1024 / 1024;
      if (isLt2M > 200) {
        this.$message.error('只允许上传200M以下的文件');
        return false;
      }
      let fileExt = file.name.slice(file.name.lastIndexOf('.') + 1);
      let fileTypeArr = ['doc', 'docx', 'pdf', 'xls', 'xlsx', 'jpg', 'png', 'jpeg', 'txt', 'et'];
      if (fileTypeArr.indexOf(fileExt) === -1) {
        this.$message.error('非法的文件类型');
        return false;
      }
      return true;
    },
    handleRemove(){
      this.fileList_tb=[]
    },
    handleGKRemove(){
      this.fileList_gk=[]
    },
    handleNOGKRemove(){
      this.fileList_nogk=[]
    },
    customUpload(file) {
      // beforeFileUpload()返回为true，才会触发这个方法；
      this.handleUpload(file);
    },
    // 构造formData
    handleUpload(file) {
      // const formData = new FormData()
      // let timestamp = new Date().getTime()
      // formData.append('files', file)
      // formData.append('fileUid', file.uid)
      // formData.append('totalBytes', file.size)
      // formData.append('time', timestamp)
      // this.imagerAdd(formData)
      getuploadFileInfo(file.file).then(res => {
        file.onSuccess(res)
        // eslint-disable-next-line no-unused-expressions
        this.fileList_tb =[]
        this.fileList_tb.push(res)
      });
    },
    handleUploadGK(file) {
      // const formData = new FormData()
      // let timestamp = new Date().getTime()
      // formData.append('files', file)
      // formData.append('fileUid', file.uid)
      // formData.append('totalBytes', file.size)
      // formData.append('time', timestamp)
      // this.imagerAdd(formData)
      getuploadFileInfo(file.file).then(res => {
        file.onSuccess(res)
        // eslint-disable-next-line no-unused-expressions
        this.fileList_gk =[]
        this.fileList_gk.push(res)
      });
    },
    handleUploadNOGK(file) {
      // const formData = new FormData()
      // let timestamp = new Date().getTime()
      // formData.append('files', file)
      // formData.append('fileUid', file.uid)
      // formData.append('totalBytes', file.size)
      // formData.append('time', timestamp)
      // this.imagerAdd(formData)
      getuploadFileInfo(file.file).then(res => {
        file.onSuccess(res)
        // eslint-disable-next-line no-unused-expressions
        this.fileList_nogk =[]
        this.fileList_nogk.push(res)
      });
    },
    getInfos(){
      getRectificationZGGKTotal().then(res=>{
        console.log("pp--",res)
        this.ruleForm=res
        this.fileList_gk=res.fileList_gk
        this.fileList_tb=res.fileList_tb
        this.fileList_nogk=res.fileList_nogk
        this.f_status=getStatusType(res.f_status)
      })
    },
    exportFile() {
      let foldid=getNowMilliSecond()
      let dir=getSystemDir()
      console.log("pp--s--",dir)
      mkdir(dir)
      mkdir(dir+"\\"+foldid)
      exportModule(5,dir+"\\"+foldid)
    },
    save () {
      this.$refs.ruleForm2.validate(valid => {
        // eslint-disable-next-line eqeqeq
        if (valid) {
          if(this.fileList_tb.length==0){
            this.$message.warn('请上传党内通报稿')
            return
          }
          if(this.fileList_gk.length==0){
            this.$message.warn('请上社会公开报稿')
            return
          }
          if(this.fileList_nogk.length==0){
            this.$message.warn('不宜向社会公开说明')
            return
          }
          this.ruleForm.fileList_tb=this.fileList_tb
          this.ruleForm.fileList_gk=this.fileList_gk
          this.ruleForm.fileList_nogk=this.fileList_nogk
          this.ruleForm.gk_status=0
          addRectificationZGGKTotal(this.ruleForm).then(res => {
            if (res === 0) {
              this.$message.error('保存失败')
              return
            }
            this.$message.success('保存成功')
          })
        }
      })
    },
    downtmp(type){
      getTemplete().then(res=>{
        const ls= res.filter(function (value) {
          return value.F_MBFL_==(type+"")
        })
        if(ls.length==0||ls[0].paths===undefined){
          this.$message.warn("未查询到模板")
        }else{
          console.log('fsname-----' ,ls);
          let fileb = fs.createReadStream(ls[0].paths);
          let fsname = ls[0].fname;

          streamToBuffer(fileb).then(res => {
            let data = res;
            let contents = new Uint8Array(data.length);
            for (let i = 0; i < data.length; i++) {
              contents[i] = data[i];
            }
            // eslint-disable-next-line no-undef
            let expname = fsname;
            saveAs.saveAs(new Blob([contents], { type: 'application/zip' }), expname);
          });
        }
      })
    },
    getstatus(){
      const plantotal_dbson=JSON.parse(fs.readFileSync(getAppLocalPath()+"dbson/RectificationZGGKAllinfos.dbson","utf-8")==""?"[]":fs.readFileSync(getAppLocalPath()+"dbson/RectificationZGGKAllinfos.dbson","utf-8"))
      console.log("stas---",plantotal_dbson)

      if(plantotal_dbson.length==0){
        this.m_status=="0"
        return;
      }
      let lsarr = plantotal_dbson.filter(function(value){
        //筛选条件
        return value.jlid===getJLID()
      })
      if(lsarr.length==0){
        this.m_status=="0"
        return;
      }
      console.log("stas2---",lsarr)
      this.m_status=lsarr[0].planTotal.f_status

    }
  },
};
</script>
<style scoped lang="less">
.save {
  margin: 40px auto;
}
.tip {
  height: 26px;
  font-family: PingFangSC-Regular;
  font-size: 16px;
  font-weight: normal;
  font-stretch: normal;
  line-height: 26px;
  letter-spacing: 0px;
  color: #666666;
}
.download{
  background: #fff!important;
  color:#2377ff!important;
  border: 1px solid #2377ff;
}

.upload {
  position: relative;
  /deep/ .ant-upload-list {
    position: absolute;
    left: 100px;
    top: -10px
  }
}


/deep/ .ant-form-item-label {
  width: 176px;
}

/deep/ .ant-input {
  width: 260px
}
</style>
